package com.yunke.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunke.common.result.YunkeResult;
import com.yunke.entity.dto.YunkeConfigDto;
import com.yunke.entity.vo.YunkeConfigVo;
import com.yunke.service.YunkeConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 系统配置管理员控制器
 * 
 * @author yunke
 * @since 1.0.0
 */
@RestController
@RequestMapping("/admin/config")
@Tag(name = "系统配置管理(管理员)", description = "系统配置管理员接口")
@SaCheckLogin
@SaCheckRole("admin")
public class YunkeConfigAdminController {

    @Autowired
    private YunkeConfigService configService;

    @PostMapping
    @Operation(summary = "创建配置")
    public YunkeResult<Boolean> createConfig(@Valid @RequestBody YunkeConfigDto configDto) {
        boolean result = configService.createConfig(configDto);
        return YunkeResult.success(result);
    }

    @PutMapping
    @Operation(summary = "更新配置")
    public YunkeResult<Boolean> updateConfig(@Valid @RequestBody YunkeConfigDto configDto) {
        boolean result = configService.updateConfig(configDto);
        return YunkeResult.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除配置")
    public YunkeResult<Boolean> deleteConfig(
            @Parameter(description = "配置ID") @PathVariable Long id) {
        
        boolean result = configService.deleteConfig(id);
        return YunkeResult.success(result);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除配置")
    public YunkeResult<Boolean> deleteConfigs(
            @Parameter(description = "配置ID列表") @RequestBody List<Long> ids) {
        
        boolean result = configService.deleteConfigs(ids);
        return YunkeResult.success(result);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询配置列表")
    public YunkeResult<IPage<YunkeConfigVo>> getConfigPage(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "配置分类") @RequestParam(required = false) String configCategory,
            @Parameter(description = "配置组") @RequestParam(required = false) String configGroup,
            @Parameter(description = "配置名称") @RequestParam(required = false) String configName,
            @Parameter(description = "是否系统配置") @RequestParam(required = false) Boolean isSystem,
            @Parameter(description = "是否启用") @RequestParam(required = false) Boolean isEnabled) {
        
        IPage<YunkeConfigVo> result = configService.getConfigPage(current, size, 
            configCategory, configGroup, configName, isSystem, isEnabled);
        return YunkeResult.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询配置详情")
    public YunkeResult<YunkeConfigVo> getConfigById(
            @Parameter(description = "配置ID") @PathVariable Long id) {
        
        YunkeConfigVo config = configService.getConfigById(id);
        return YunkeResult.success(config);
    }

    @GetMapping("/system")
    @Operation(summary = "查询系统配置列表")
    public YunkeResult<List<YunkeConfigVo>> getSystemConfigs(
            @Parameter(description = "是否启用") @RequestParam(required = false) Boolean isEnabled) {
        
        List<YunkeConfigVo> configs = configService.getSystemConfigs(isEnabled);
        return YunkeResult.success(configs);
    }

    @PutMapping("/{id}/enable")
    @Operation(summary = "启用配置")
    public YunkeResult<Boolean> enableConfig(
            @Parameter(description = "配置ID") @PathVariable Long id) {
        
        boolean result = configService.enableConfig(id);
        return YunkeResult.success(result);
    }

    @PutMapping("/{id}/disable")
    @Operation(summary = "禁用配置")
    public YunkeResult<Boolean> disableConfig(
            @Parameter(description = "配置ID") @PathVariable Long id) {
        
        boolean result = configService.disableConfig(id);
        return YunkeResult.success(result);
    }

    @PutMapping("/status")
    @Operation(summary = "批量更新配置状态")
    public YunkeResult<Boolean> updateConfigStatus(
            @Parameter(description = "配置ID列表") @RequestParam List<Long> ids,
            @Parameter(description = "状态") @RequestParam Integer status) {
        
        boolean result = configService.updateConfigStatus(ids, status);
        return YunkeResult.success(result);
    }

    @PutMapping("/value/{configKey}")
    @Operation(summary = "更新配置值")
    public YunkeResult<Boolean> updateConfigValue(
            @Parameter(description = "配置键名") @PathVariable String configKey,
            @Parameter(description = "配置值") @RequestBody String configValue) {
        
        boolean result = configService.updateConfigValue(configKey, configValue);
        return YunkeResult.success(result);
    }

    @PutMapping("/values")
    @Operation(summary = "批量更新配置值")
    public YunkeResult<Boolean> updateConfigValues(
            @Parameter(description = "配置键值对") @RequestBody Map<String, String> configMap) {
        
        boolean result = configService.updateConfigValues(configMap);
        return YunkeResult.success(result);
    }

    @PutMapping("/reset/{configKey}")
    @Operation(summary = "重置配置为默认值")
    public YunkeResult<Boolean> resetConfigToDefault(
            @Parameter(description = "配置键名") @PathVariable String configKey) {
        
        boolean result = configService.resetConfigToDefault(configKey);
        return YunkeResult.success(result);
    }

    @GetMapping("/export")
    @Operation(summary = "导出配置")
    public YunkeResult<List<YunkeConfigVo>> exportConfigs(
            @Parameter(description = "配置分类") @RequestParam(required = false) String configCategory,
            @Parameter(description = "是否系统配置") @RequestParam(required = false) Boolean isSystem) {
        
        List<YunkeConfigVo> configs = configService.exportConfigs(configCategory, isSystem);
        return YunkeResult.success(configs);
    }

    @PostMapping("/import")
    @Operation(summary = "导入配置")
    public YunkeResult<Boolean> importConfigs(
            @Parameter(description = "配置列表") @RequestBody List<YunkeConfigDto> configList) {
        
        boolean result = configService.importConfigs(configList);
        return YunkeResult.success(result);
    }

    @PostMapping("/validate")
    @Operation(summary = "验证配置值")
    public YunkeResult<Boolean> validateConfigValue(
            @Parameter(description = "配置键名") @RequestParam String configKey,
            @Parameter(description = "配置值") @RequestParam String configValue) {
        
        boolean result = configService.validateConfigValue(configKey, configValue);
        return YunkeResult.success(result);
    }
} 